package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.enums.MarketingConstant;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyTeamMember;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyTeamMemberService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.GoodsMessage;
import com.songlanyun.modules.message.model.PayMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;


/**
 * 订单支付成功（除支付卡订单以外的订单）  --  支付消息
 */
@Service("orderInteractService")
public class OrderInteractServiceImpl implements IInteractMsgService {

    @Resource
    private OrderService orderService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GroupBuyTeamMemberService groupBuyTeamMemberService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {
        Order order = orderService.getById(to.getModelId());
        if (order == null) {
            return;
        }
        //发送站内消息    --给下单会员
        String remark = String.format("你已购买%d件商品，支付%s元，点击查看详情", order.getGoodsNumber(), order.getTotalAmount());
        List<GoodsMessage> goodsList = new ArrayList<>();

        //增加子订单中的商品
        List<ChildOrder> childOrderList = order.getChildOrderList(true);

        //发送mq消息
        PayMessage payMessage = new PayMessage();
        GoodsConstant.GoodsType goodsType = GoodsConstant.GoodsType.GENERAL_GOODS;//商品类型
        Integer goodsNum = order.getGoodsNumber();//商品数量
        BigDecimal goodsAmount = order.getGoodsAmount();//商品金额
        BigDecimal totalPoint = BigDecimal.ZERO;//总积分

        if (MarketingConstant.ActivityType.NOT.getCode().equals(order.getMarketingType())) {
            //无活动商品
            childOrderList.forEach(c -> {
                GoodsMessage goodsMessage = new GoodsMessage();
                goodsMessage.setGoodsId(c.getGoodsId());
                goodsMessage.setGoodsName(c.getGoodsTitle());
                goodsMessage.setGoodsImg(c.getGoodsImg());
                goodsMessage.setGoodsSpec(c.getSkuSetName());
                goodsList.add(goodsMessage);
            });

            if (order.getOrderType().equals(OrderConstant.OrderType.SERVICE_ORDER)) {
                //服务商品
                goodsType = GoodsConstant.GoodsType.SERVICE_GOODS;
            }
            if (order.getExchange()) {
                //积分商品
                goodsType = GoodsConstant.GoodsType.INTEGRAL_GOODS;
                totalPoint = order.getTotalPoint();
                remark = String.format("你已购买%d件商品，支付积分%d,支付运费%s元，点击查看详情", order.getGoodsNumber(), order.getTotalPoint(), order.getTotalAmount());
            }
        } else {
            //活动商品
            if (MarketingConstant.ActivityType.SECKILL.getCode().equals(order.getMarketingType())) {
                //秒杀商品
                childOrderList.forEach(c -> {
                    GoodsMessage goodsMessage = new GoodsMessage(c.getActivityGoodsId(), c.getGoodsTitle(), c.getGoodsImg(),
                            c.getSkuSetName());
                    goodsList.add(goodsMessage);
                });
                goodsType = GoodsConstant.GoodsType.ACTIVITY_GOODS;
                payMessage.setActivityType(MarketingConstant.ActivityType.SECKILL);
            }
            if (MarketingConstant.ActivityType.GROUP_BUY.getCode().equals(order.getMarketingType())) {
                //拼团商品
                childOrderList.forEach(c -> {
                    GoodsMessage goodsMessage = new GoodsMessage(c.getActivityGoodsId(), c.getGoodsTitle(), c.getGoodsImg(),
                            c.getSkuSetName());
                    GroupBuyTeamMember teamMember = groupBuyTeamMemberService.getByOrderIdAndUserId(c.getOrderId(), c.getUserId());
                    goodsMessage.setTeamMemberId(teamMember.getId());
                    goodsList.add(goodsMessage);
                });
                goodsType = GoodsConstant.GoodsType.ACTIVITY_GOODS;
                payMessage.setActivityType(MarketingConstant.ActivityType.GROUP_BUY);
            }
        }

        payMessage.setOrderId(order.getId());
        payMessage.setGoodsType(goodsType);//商品类型
        payMessage.setGoodsNum(goodsNum);//商品数量
        if (childOrderList.size() == 1) {//同件商品时，金额取子订单的销售价
            payMessage.setMoney(childOrderList.get(0).getSalePrice());//商品金额
        } else {
            payMessage.setMoney(order.getGoodsAmount());//商品金额
        }
        payMessage.setTotalPoint(totalPoint);//总积分
        payMessage.setGoods(goodsList);//商品
        payMessage.setTotalAmount(order.getTotalAmount());//支付总金额

        PrivateMessageTO privateMessageTO = new PrivateMessageTO(order.getUserId(), PrivateMessageConstant.MessageType.PAY_MESSAGE, payMessage, remark);
        PrivateMessage privateMessage = new PrivateMessage(privateMessageTO);

        privateMessageService.save(privateMessage);

        //单推
        geTuiUtils.pushByUserId(
                Msg.builder()
                        .uid(order.getUserId())
                        .title(PrivateMessageConstant.MessageType.PAY_MESSAGE.getMsg())
                        .content(remark)
                        .msg(privateMessage.pushMessageBody())
                        .build()
        );

    }
}
